package ProjectManagement;

import com.spm.pojo.StudentGrade;
import com.spm.pojo.Teacher;
import com.spm.service.GradeService;
import com.spm.service.impl.GradeServiceImpl;
import com.spm.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "ShowAllGradeServlet" ,value="/ShowAllGradeServlet")
public class ShowAllGradeServlet extends HttpServlet {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @SuppressWarnings("unchecked")
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GradeService gradeService = new GradeServiceImpl(sqlSession);
        HttpSession session = request.getSession();
        Teacher teacher = (Teacher) session.getAttribute("teacher");
//        //从教师类中获取教师编号
        String tid = teacher.getTid();
//
//        //2、从request中获取cno参数
        String cno = request.getParameter("cno");
//
//        //3、调用gradeService获取成绩列表
//        List<StudentGrade> studentGrades = gradeService.queryByCourse(tid, cno);
//        sqlSession.close();
        List<StudentGrade> studentGrades = gradeService.queryByCourse(tid, cno);
        //4、保存studentGrades至request域中

        request.setAttribute("gradesOfCourse", studentGrades);

        for (StudentGrade s :
                studentGrades) {
            System.out.println(s);
        }

        //5、请求转发到...页面
        String mainPage = "AllGradeDisplays.jsp";
        request.setAttribute("mainPage", mainPage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ProjectManagement/main.jsp");
        dispatcher.forward(request, response);
    }

}